package cn.afactory.tygz.ui.base.adapter;

import android.content.Context;
import android.media.Image;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.harwkin.nb.camera.BaseImgView;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.SnsCommentInfo;
import cn.afactory.tygz.ui.base.entity.CommunityInfo;
import cn.afactory.tygz.ui.base.utils.DateUtil;
import cn.afactory.tygz.ui.base.utils.ImageUtils;

/**
 * Created by Kenneth on 15/9/15.
 */
public class CommentListAdapter extends BaseListAdapter<SnsCommentInfo, CommentListAdapter.CommentViewHolder>{

    public void setListType(int listType) {
        this.listType = listType;
    }

    private int listType = 0; // 0为帖子评论，1为社区评论

    NearByForumsAdapter.OnItemClickListener mItemClickListener;
    public void setOnItemClickLsn(NearByForumsAdapter.OnItemClickListener lsn){
        mItemClickListener = lsn;
    }

    public CommentListAdapter(Context context, List<SnsCommentInfo> listData) {
        super(context, listData);
    }

    @Override
    public View newView(int position, LayoutInflater inflater, ViewGroup parent) {
        if (this.listType == 0) {
            return inflater.inflate(R.layout.fg_near_by_forums_comment_item, null);
        } else {
            return inflater.inflate(R.layout.fg_community_comment_item, null);
        }
    }

    @Override
    public CommentViewHolder onViewHolderCreate(int position, View view) {
        CommentViewHolder viewHolder = new CommentViewHolder();
        viewHolder.userIcon = (ImageView)view.findViewById(R.id.comment_user_icon);
        viewHolder.userName = (TextView)view.findViewById(R.id.comment_user_name);
        viewHolder.userAge = (TextView)view.findViewById(R.id.comment_user_age);
        viewHolder.genderIcon = (ImageView)view.findViewById(R.id.comment_user_gender);
        viewHolder.userAgeRight = (View)view.findViewById(R.id.comment_user_age_right);
        viewHolder.userCity = (TextView)view.findViewById(R.id.comment_user_city);
        viewHolder.commentContent = (TextView)view.findViewById(R.id.comment_content);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(int position, final SnsCommentInfo itemData, CommentViewHolder commentViewHolder) {

        if (!TextUtils.isEmpty(itemData.userIcon)) {
            commentViewHolder.userIcon.setScaleType(ImageView.ScaleType.CENTER_CROP);
            int nwidth = 24;
            BaseImgView.loadimg(commentViewHolder.userIcon, ImageUtils.getThumbnailFullPath(itemData.userIcon,
                            nwidth + "x" +
                                    nwidth),
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    ImageScaleType.EXACTLY,
                    nwidth,
                    nwidth, 0);
        } else {
            commentViewHolder.userIcon.setImageResource(R.drawable.ic_pic_loading_default_small);
        }
        commentViewHolder.userIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mItemClickListener) {
                    mItemClickListener.onClickCommentIcon(itemData);
                }
            }
        });

        if (!TextUtils.isEmpty(itemData.userName)) {
            commentViewHolder.userName.setText(itemData.userName);
            commentViewHolder.userName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != mItemClickListener) {
                        mItemClickListener.onClickCommentIcon(itemData);
                    }
                }
            });
        } else {
            commentViewHolder.userName.setText("");
        }

        if (!TextUtils.isEmpty(itemData.cityName) && itemData.birth != 0) {
            commentViewHolder.userAgeRight.setVisibility(View.VISIBLE);
        } else {
            commentViewHolder.userAgeRight.setVisibility(View.GONE);
        }

        if (itemData.birth != 0) {
            commentViewHolder.userAge.setVisibility(View.VISIBLE);
            commentViewHolder.userAge.setText(DateUtil.getAgeByNorth(itemData.birth) + "岁");
        } else {
            commentViewHolder.userAge.setVisibility(View.VISIBLE);
        }

        commentViewHolder.genderIcon.setVisibility(View.VISIBLE);
        if (itemData.sex == 0){
            commentViewHolder.genderIcon.setImageResource(R.drawable.tag_male);
        } else {
            commentViewHolder.genderIcon.setImageResource(R.drawable.tag_female);
        }

        if (!TextUtils.isEmpty(itemData.cityName)) {
            commentViewHolder.userCity.setVisibility(View.VISIBLE);
            commentViewHolder.userCity.setText(itemData.cityName);
        } else {
            commentViewHolder.userCity.setVisibility(View.VISIBLE);
            commentViewHolder.userCity.setText("");
        }

        if (null != itemData.content) {
            commentViewHolder.commentContent.setText(itemData.content);
        }

    }

    public class CommentViewHolder extends BaseListAdapter.ViewHolder {
        ImageView userIcon;
        TextView userName;
        TextView userAge;
        ImageView genderIcon;
        View userAgeRight;
        TextView userCity;
        TextView commentContent;
    }
}
